package com.YxyBackMange.Controller.Ware;


import com.YxyBackMange.Dto.Message;
import com.YxyBackMange.Entity.Ware.WareOrderInfo;
import com.YxyBackMange.Mapper.Ware.WareOrderInfoMapper;
import com.YxyBackMange.Service.Ware.WareOrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/Admin/Ware/WareOrderInfo")
public class  WareOrderInfoController {

    private Logger logger= LoggerFactory.getLogger(WareOrderInfoController.class);

    @Autowired
    private WareOrderInfoService wareOrderInfoService;

    @Autowired
    private  WareOrderInfoMapper wareOrderInfoMapper;

    @RequestMapping("/listPage")
    public String listPage(HttpSession session, ModelMap map){

        return "/admin/page/Ware/WareOrderInfo/listWareOrderInfoPage.jsp";

    }
    @RequestMapping("/addPage")
    public String addPage(HttpSession session, ModelMap map){

        return "/admin/page/Ware/WareOrderInfo/updateWareOrderInfoPage.jsp";
    }
    @RequestMapping("/detailPage")
    public String detailPage(HttpSession session, @RequestParam Integer id, ModelMap map){
        WareOrderInfo wareOrderInfo=new WareOrderInfo(id);
        wareOrderInfo=wareOrderInfoMapper.selectByPrimaryKey(wareOrderInfo);
        map.put("obj",wareOrderInfo);
        return "/admin/page/Ware/WareOrderInfo/detailWareOrderInfoPage.jsp";

    }
    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session, @RequestParam Integer id, ModelMap map){

        WareOrderInfo wareOrderInfo=new WareOrderInfo(id);
        wareOrderInfo=wareOrderInfoMapper.selectByPrimaryKey(wareOrderInfo);
        map.put("obj",wareOrderInfo);
        return "/admin/page/Ware/WareOrderInfo/updateWareOrderInfoPage.jsp";
    }
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public Message addOrUpdate(WareOrderInfo wareOrderInfo,HttpSession session )
    {
        Message msg=new Message();
        try{
            if(wareOrderInfo!=null&&wareOrderInfo.getId()!=null)
            {
                 wareOrderInfo=wareOrderInfoService.updateService(wareOrderInfo);
            }else{

                  wareOrderInfo=wareOrderInfoService.insertService(wareOrderInfo);
            }
            if(wareOrderInfo==null)
            {
                msg.setMsg("操作失败,您可以稍后重试!");
                return msg;
            }
            msg.setData(wareOrderInfoMapper.selectQueryById(wareOrderInfo.getId()));
            msg.setMsg("保存成功！");
            msg.setState(true);
            logger.info("保存成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("保存失败，请稍后重试");
            e.printStackTrace();
            logger.error("保存失败:"+e.getMessage());
            return msg;
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public Message update(WareOrderInfo wareOrderInfo,HttpSession session)
    {
        Message msg=new Message();
        try{
            wareOrderInfoService.update(wareOrderInfo);
            msg.setData(wareOrderInfoMapper.selectByPrimaryKey(wareOrderInfo));
            msg.setMsg("更新成功！");
            msg.setState(true);
             logger.info("更新成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("更新失败，请稍后重试");
            e.printStackTrace();
            logger.error("更新失败:"+e.getMessage());
            return msg;
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestParam Integer id, HttpSession session)
    {
        Message msg=new Message();
        try{
            WareOrderInfo wareOrderInfo=new WareOrderInfo(id);
            wareOrderInfoService.delete(wareOrderInfo);
            msg.setData(wareOrderInfo);
            msg.setMsg("删除成功！");
            msg.setState(true);
            logger.error("删除成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("删除失败，请稍后重试");
            logger.error("更新失败:"+e.getMessage());
            e.printStackTrace();
            return msg;
        }
    }
}
